Ext.ns("CRM.SPComp");

(function(){

	
	
			//search component
	
			CRM.SPComp.createActivitySearchComp = function(gridId, criteriaComboId, criteriaTextfieldId, dataStore, columnModel, criteriaStore){
	       	                           	                             		
	     	var gridView = new Ext.grid.GridView();
	     	var selModel = new Ext.grid.RowSelectionModel({singleSelect : true});
	     	var spSearchActivityGridPanel = new Ext.grid.GridPanel({
	       	      autoHeight 	: true,
	       	      id			: gridId, //'spSearchActivityGridPanel',
	       	      width			: 1060,	
	       	      title			: 'Search Results',
	       	      view 			: gridView,
	       	      store			: dataStore,
	       	      colModel 		: columnModel,		
	       	      selModel 		: selModel,
	       	      stripeRows	: true,
	       	      enableRowBody : true,
	       	      scrollable	: true,
	       	      frame			: true
	       	   });		
		
	     	var dynamicGridPanel = new Ext.Panel({
	     		width    : 1085,
	     		items 	: [
							{
								xtype		: 'panel',
								id			: 'combinePanel',
								autoHeight 	: true,
								width		: 1085,
								frame		: true,
								padding		: '5px',
								items		: [
												 {
												 xtype       : 'fieldset',
											     id          : 'criteriaPanel',
											     title       : 'Search Criteria',
											     height      : 65,
											     width       : 1060,
											     labelWidth  : 120,
											     layout      : 'hbox',
											     items       : [
											                      {
											                    	 xtype : 'compositefield',
											                    	 items : [	
											                    				{ xtype : 'displayfield', value : 'Search Criteria : '},
											                       				{
											                    	   				xtype 		: 'combo',
											                    	   				id	  		: criteriaComboId, //'searchCriteria',
											                    	   				store		: criteriaStore,
											                                        displayField: 'criteria',
											                                        valueField  : 'id',
											                                        value		: 'SELECT',
											                                        triggerAction:'all',
											                    	   				width 		: 200,
											                    	   				editable	: false
											                       				},
											                       				{ xtype : 'displayfield', value : 'Value : '},
											                       				{ 
											                       					xtype : 'textfield',
											                       					id	  : criteriaTextfieldId, //'criteriaValue',
											                       					width : 350,
											                       					enableKeyEvents: true,
											                       					listeners: {keyup: function(){
											                       										doSearch(true);
											                       									}
											                       								}	
											                       				},
											                       				{
											                       					xtype : 'button',
											                       					text  : 'Search',
											                       					handler : function(){
											                       								doSearch(false);
											                       							}
											                       				},
											                       				{
											                       					xtype	: 'button',
											                       					text	: 'Clear Search',
											                       					handler : function(){
											                       								var searchGrd = Ext.getCmp(gridId);
											                       								searchGrd.store.removeAll();
											                       								}
											                       				}
											                    			]
											                       }
																 ]
												 		},
														spSearchActivityGridPanel
												 ]
							}
						]
	     	
	     	});
	     	
	     	function doSearch(ONKEYEVENT){
	     		
	     		var selectedCriteria = Ext.getCmp(criteriaComboId).getValue();
				var passedValue = Ext.getCmp(criteriaTextfieldId).getValue();
	     		
	     		if(selectedCriteria == 'SELECT'){
 					Ext.Msg.alert('Alert','Please Select a Criteria');
 				}
 				
 				if(ONKEYEVENT == false){
 					if(passedValue == '' ){
 						Ext.Msg.alert('Alert','Please Enter Some Value to Search');
 					}
 				}
 				if(selectedCriteria != 'SELECT' && passedValue != ''){
						var searchGrd = Ext.getCmp(gridId);
						searchGrd.store.removeAll();
						searchGrd.store.reload({
			        			params: {
			        						criteria: selectedCriteria,
			        						value: passedValue
			        					}
			        		});
 				}	
	     	}
		
		return dynamicGridPanel;
	};

})();